/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package medium;
import java.io.*;
import common.Convertor;
import common.Switcher;
import eac.ucm.AddValue;
import eac.ucm.DeleteValue;
import eac.ucm.RefreshTable;
import javax.swing.JOptionPane;
import library.ucm.LendBook;
import library.ucm.RefreshTableLIB;
import library.ucm.SetDeliveryDeskPhoto;
import library.ucm.SetDeliveryDeskValue;
import library.view.Menu;
import main.view.MainTable;

/**
 *
 * @author Donghui
 */

public class MsgReader extends Thread{

    private BufferedReader reader;
    private String message;
    private PrintWriter sender;
    private String[] spliter;
    private String[] data=new String[20];
    private String[] data1=new String[5];
    private static MsgReader local;

//    private EACenter eac;
//                data[0]=spliter[0];
//                data[1]=spliter[1];
//                data[2]=spliter[2];
//                data[3]=spliter[3];
//                data[4]=spliter[4];
//                data[5]=spliter[5];
//                data[6]=spliter[6];
//                data[7]=spliter[7];
//                data[8]=spliter[8];
//                data[9]=spliter[9];

    public static MsgReader getInstance(BufferedReader tpa, PrintWriter tpb)
    {
        if(local==null)
        {
            local=new MsgReader();
            local.reader=tpa;
            local.sender=tpb;
        }
        else
        {
            local.reader=tpa;
            local.sender=tpb;
        }
        return local;
    }

    public void to1(String tp)
    {
        spliter=tp.split(";");
        data[0]=spliter[0];
        data1[0]=data[1]=spliter[1];
        data1[1]=data[2]=spliter[2];
        data1[2]=data[3]=spliter[3];
        data1[3]=data[4]=spliter[4];
        data1[4]=data[5]=spliter[5];

        if(data[0].equalsIgnoreCase("Y"))
        {
            RefreshTable.getInstance().showAchievement(data1);
        }
        else
        {
            System.out.println("Failed to query student's achievement");
        }
    }

    public void to2(String tp) {
        spliter=tp.split(";");
        data[0]=spliter[0];
        AddValue.getInstance(data[0]).stResponse();
    }

    public void to3(String tp)
    {
        spliter=tp.split(";");
        data[0]=spliter[0];
        AddValue.getInstance(data[0]).tResponse();
    }


    public void to4(String tp) {
        spliter=tp.split(";");
        data[0] = spliter[0];
        data[1] = spliter[1];
        data[1] = Convertor.getInstance().rollGroup(Integer.valueOf(data[1]));
        System.out.println(data[1]);
        if(data[1].equalsIgnoreCase("Teacher"))
        {
            data[2] = spliter[2];
            data[2] = Convertor.getInstance().teacherGroup(Integer.valueOf(data[2]));
            System.out.println(data[2]);
            if (data[0].equalsIgnoreCase("Y")) {
                MainTable.getInstance();
            } else {
                System.out.println("Failed to pass verify");
            }
        }
        if(data[1].equalsIgnoreCase("Staff"))
        {
        data[2] = spliter[2];
        data[2] = Convertor.getInstance().staffGroup(Integer.valueOf(data[2]));
        System.out.println(data[2]);
            if (data[0].equalsIgnoreCase("Y")) {
                    if (data[2].equalsIgnoreCase("Educational Administration Center"))
                    {
                        Switcher.getInstance();
                    }
                    if (data[2].equalsIgnoreCase("Library"))
                    {
                        Menu.getInstance();
                    }
            } else {
                System.out.println("Failed to pass verify");
            }
        }
        if(data[1].equalsIgnoreCase("Student"))
        {
        data[2] = spliter[2];
        data[2] = Convertor.getInstance().studentGroup(Integer.valueOf(data[2]));
        System.out.println(data[2]);
            if (data[0].equalsIgnoreCase("Y")) {
                MainTable.getInstance();
            } else {
                System.out.println("Failed to pass verify");
            }
        }
    }

    public void to5(String tp)
    {
        spliter=tp.split(";");
        data[0]=spliter[0];
        if(data[0].equalsIgnoreCase("Y"))
        {
            DeleteValue.getInstance().stResponse();
        }
    }

    public void to6(String tp)
    {
        spliter=tp.split(";");
        data[0]=spliter[0];
        data1[0]=data[1]=spliter[1];
        data1[1]=data[2]=spliter[2];
        data1[2]=data[3]=spliter[3];
        data1[3]=data[4]=spliter[4];
        data1[4]=data[5]=spliter[5];
        data1[4]=Convertor.getInstance().college(Integer.valueOf(data1[4]));

        if(data[0].equalsIgnoreCase("Y"))
        {

            RefreshTable.getInstance().showCourse(data1);
        }
        else
        {
            System.out.println("Failed to query student's achievement");
        }
    }

    public void to7(String tp)
    {
        spliter=tp.split(";");
        data[0]=spliter[0];
        if(data[0].equalsIgnoreCase("Y"))
        {
            DeleteValue.getInstance().tResponse();
        }
        else
        {
            System.out.println("Failed to delete course");
        }
    }

    public void to8(String tp)
    {
        spliter=tp.split(";");
        data[0]=spliter[0];
        if(data[0].equalsIgnoreCase("Y"))
        {
            SetDeliveryDeskValue.getInstance(tp).setValue();
            SetDeliveryDeskPhoto.getInstance(tp).setPhoto();
        }
        else
        {
            SetDeliveryDeskPhoto.getInstance(tp).setNull();
            SetDeliveryDeskValue.getInstance(tp).setNull();
            System.out.println("Failed to pass verify");
        }
    }

    public void to9(String tp)
    {
        spliter=tp.split(";");
        data[0]=spliter[0];
        data1[0]=spliter[1];
        data1[1]=spliter[2];
        data1[2]=spliter[3];
        data1[3]=spliter[4];
        data1[4]=spliter[5];
        if(data[0].equalsIgnoreCase("Y"))
        {
            RefreshTableLIB.getInstance().initTable();
            RefreshTableLIB.getInstance().showBook(data1);
        }
        else
        {
            
            System.out.println("Failed to pass verify");
        }
    }

    public void to10(String tp)
    {
        spliter=tp.split(";");
        data[0]=spliter[0];
        if(data[0].equalsIgnoreCase("Y"))
        {
            LendBook.getInstance().response();
        }
        else
        {
            LendBook.getInstance().responseFailed();
            System.out.println("Failed to pass verify");
        }
    }

    public void to11(String tp)
    {
        spliter=tp.split(";");
        data[0]=spliter[0];
        if(data[0].equalsIgnoreCase("Y"))
        {
            JOptionPane.showMessageDialog(null,"Updated successfully");
        }
        else
        {
            JOptionPane.showMessageDialog(null,"Failed to update");
        }
    }

    @Override
    public void run()
    {
        while(true)
        {
            try {
                System.out.println("Waitting for response from server");
                message = reader.readLine();
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(!message.equals("null"))
            {
                System.out.println("System: "+message);
                spliter=message.split(";");
                switch(Integer.valueOf(spliter[spliter.length-1]))
                {
                    case 1: System.out.println("CASE 1");local.to1(message);break;
                    case 3: System.out.println("CASE 3");local.to3(message);break;
                    case 2: System.out.println("CASE 2");local.to2(message);break;
                    case 4: System.out.println("CASE 4");local.to4(message);break;
                    case 5: System.out.println("CASE 5");local.to5(message);break;
                    case 6: System.out.println("CASE 6");local.to6(message);break;
                    case 7: System.out.println("CASE 7");local.to7(message);break;
                    case 8: System.out.println("CASE 8");local.to8(message);break;
                    case 9: System.out.println("CASE 9");local.to9(message);break;
                    case 10: System.out.println("CASE 10");local.to10(message);break;
                    case 11: System.out.println("CASE 11");local.to11(message);break;
                }
            }
        }
    }

}
